package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVOSCloud;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.RequestMobileCodeCallback;
import com.avos.avoscloud.SaveCallback;
import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.StringUtils;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.widget.ClearEditText;
import com.wqtz.chechai.widget.CountDownTimerUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/20 13:55
 */

public class InputNewPhoneNumberActivity extends BaseActivity {
	@BindView(R.id.iv_back)
	ImageView mIvBack;
	@BindView(R.id.tv_title)
	TextView mTvTitle;
	@BindView(R.id.tv_notice)
	TextView mTvNotice;
	@BindView(R.id.tv_input_notice)
	TextView mTvInputNotice;
	@BindView(R.id.tv_input)
	ClearEditText mTvInput;
	@BindView(R.id.tv_sendmsm)
	TextView mTvSendmsm;
	@BindView(R.id.btn_go_next)
	Button mBtnGoNext;
	@BindView(R.id.tv_notice_phone)
	TextView mphone;
	String phone;

	@Override
	public void init() {
		ButterKnife.bind(this);
		Intent intent = getIntent();
		phone = intent.getStringExtra("phone");
		mphone.setText(phone);
	}

	@Override
	public void loadData() {
		mTvInput.addTextChangedListener(watcher);
	}

	@Override
	public int setContentViews() {
		return R.layout.activity_sign_phone_number;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}


	@OnClick({R.id.iv_back, R.id.tv_sendmsm, R.id.btn_go_next})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.iv_back:
				finish();
				break;
			case R.id.tv_sendmsm:
				CountDownTimerUtils mCountDownTimerUtils = new CountDownTimerUtils(mTvSendmsm, 60000, 1000);
				mCountDownTimerUtils.start();
				AVOSCloud.requestSMSCodeInBackground(phone, new RequestMobileCodeCallback() {
					@Override
					public void done(AVException e) {
						// 发送失败可以查看 e 里面提供的信息
					}
				});
				break;
			case R.id.btn_go_next:
				AVUser.getCurrentUser().saveInBackground(new SaveCallback() {
					@Override
					public void done(AVException e) {
						AVUser.getCurrentUser().put("mobilePhoneNumber", phone);
						AVUser.getCurrentUser().saveInBackground();
					}
				});

				break;
		}
	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
			// TODO Auto-generated method stub
			//Logger.e(s.toString()+start+"  "+before+"  "+count);

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
		                              int after) {
			// TODO Auto-generated method stub
			//Logger.e(s.toString()+start+"  "+count);

		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			Logger.e(s.toString());
			if (s.toString().toCharArray().length == 6 && StringUtils.isNumeric(s.toString())) {
				mBtnGoNext.setClickable(true);
				mBtnGoNext.setBackgroundResource(R.drawable.shape_login_btn_pass);
			} else {
				mBtnGoNext.setClickable(false);
				mBtnGoNext.setBackgroundResource(R.drawable.shape_login_btn_default);
			}
			if (s.toString().toCharArray().length > 6) {
				ToastUtils.show(InputNewPhoneNumberActivity.this, R.string.disablenum, 2000);
			}

		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity
	}
}
